<template>
  <div class="app-content">
    <div class="search-content">
      <Title title="组合信息"></Title>

      <div class="search-input-content">
        <el-input
          v-model="queryParams.findString"
          placeholder="可查询组合名称"
          size="small"
          class="search-input"
          @keyup.enter.native="handleQuery"
        >
        </el-input>

        <i class="el-icon-search search-icon" @click="handleQuery"></i>
        <i
          class="el-icon-circle-close search-icon"
          v-if="queryParams.findString"
          @click="clear()"
        ></i>
      </div>
    </div>

    <div class="app-container">
      <el-table
        stripe
        :data="groupList"
        row-key="id"
        :expand-row-keys="expands"
      >
        <el-table-column type="expand" prop="productData">
          <template slot-scope="scope">
            <div class="tag-content">
              <div
                class="tag-item"
                :key="tag.prdtName"
                v-for="tag in scope.row.productData"
              >
                <el-tag
                  closable
                  :disable-transitions="false"
                  @close="handleClose(tag)"
                  style="margin: auto"
                  type="danger"
                  >{{ tag.prdtName }}</el-tag
                >
              </div>
              <div class="tag-item">
                <el-button
                  class="button-new-tag"
                  size="small"
                  @click="AddProduct(scope.row)"
                  >+ 新产品</el-button
                >
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="序号" prop="index" width="55">
          <template slot-scope="scope">
            <span>{{ scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="组合编号" align="center" prop="groupCode" />
        <el-table-column label="组合名称" align="center" prop="groupName" />
        <el-table-column label="产品个数" align="center" prop="productNum" />

        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
          width="150"
        >
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="handleUpdate(scope.row)"
              >编辑</el-button
            >
            <el-button size="mini" type="text" @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 新增对话框 -->
      <el-dialog title="新增" :visible.sync="open" width="450px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <el-row>
            <el-col :span="24">
              <el-form-item label="产品名称：" prop="groupName">
                <el-select
                  v-model="form.productId"
                  popper-class="select-header-popper"
                  placeholder="请选择"
                  filterable
                  style="width: 300px"
                >
                  <el-option
                    style="width: 300px"
                    v-for="item in productOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="cancel" size="small">取消</el-button>
          <el-button type="danger" @click="submitForm" size="small"
            >确定</el-button
          >
        </div>
      </el-dialog>

      <!-- 编辑对话框 -->
      <el-dialog
        title="重命名"
        :visible.sync="editOpen"
        width="430px"
        append-to-body
      >
        <el-form
          ref="editForm"
          :model="editForm"
          :rules="editRules"
          label-width="100px"
        >
          <el-row>
            <el-col :span="24">
              <el-form-item label="组合名称：" prop="groupName">
                <el-input
                  v-model="editForm.groupName"
                  placeholder="请输入组合名称"
                  size="small"
                  style="width: 280px"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="cancelEdit" size="small">取消</el-button>
          <el-button type="danger" @click="submitEdit" size="small"
            >确定</el-button
          >
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import Title from "../../components/title";
import groupList from "../../../static/product/groupList.json";
import productSelect from "../../../static/product/prodSelect.json";
import {
  getGroupList,
  addProductInGroup,
  modifyGroupName,
  deleteGroup,
} from "@/api/product/group";
import {
  getProductList,
  groupProducts,
  delGroupProduct,
} from "@/api/product/information";

export default {
  name: "order",
  components: { Title },
  data() {
    return {
      title: "订单管理",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公告表格数据
      groupList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 类型数据字典
      statusOptions: [],
      // 状态数据字典
      typeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        findString: undefined,
      },
      // 表单参数
      form: {},
      editForm: {},
      // 表单校验
      rules: {
        productId: [
          { required: true, message: "请选择产品名称", trigger: "blur" },
        ],
      },
      editRules: {
        groupName: [
          { required: true, message: "请输入产品名称", trigger: "blur" },
        ],
      },
      groupId: "", // 选中添加的组合ID
      productOptions: [],
      editOpen: false,
      // 要展开的行，数值的元素是row的key值
      expands: [],
    };
  },
  created() {
    this.getList();
    this.getSelectList();
  },
  methods: {
    // 查询产组合信息列表
    getList() {
      // this.loading = true;
      this.groupList = groupList.rows;
      this.total = groupList.total;
      // getGroupList(this.queryParams).then((response) => {
      //   this.groupList = response.rows;
      //   this.total = response.total;
      //   this.loading = false;
      // });
    },
    getSelectList() {
      // let data = productSelect.rows.filter((item) => item.isGroup != 1);
      let dataTemp = [];
      productSelect.rows.forEach(function (e) {
        if (e.isGroup == 0) {
          dataTemp.push({
            value: e.id,
            label: e.prdtName,
          });
        }
      });
      this.productOptions = dataTemp;
      // getProductList().then((response) => {
      //   // 过滤出没有组合过的产品
      //   // let data = response.rows.filter(item=>item.isGroup!=1)
      //   let dataTemp = [];
      //   response.rows.forEach(function (e) {
      //     if (e.isGroup == 0) {
      //       dataTemp.push({
      //         value: e.id,
      //         label: e.prdtName,
      //       });
      //     }
      //   });
      //   this.productOptions = dataTemp;
      //   // console.log(data);
      // });
    },
    // 添加产品对话框
    AddProduct(row) {
      this.open = true;
      this.groupId = row.id;
      // 表格置空
      this.form = {};
      console.log(this.expands, "[[[[[[expends]]]]]]]]]");
      this.expands = [];
      this.expands.push(row.id);
      console.log(this.expands, "[[[[[[expends]]]]]]]]]");
    },
    handleClose(tag) {
      console.log(tag);
      this.$confirm("确定删除改产品？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delGroupProduct(tag.id).then((res) => {
            // console.log()
            if (res.code == 200) {
              this.$message.success("删除成功");
              this.getList();
            } else {
              this.$message.error(res.msg);
            }
          });
          // this.$store.dispatch("LogOut").then(() => {
          //   location.href = "/index";
          // });
        })
        .catch(() => {});
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        groupName: undefined,
      };
      this.resetForm("form");
    },
    // 取消按钮
    cancelEdit() {
      this.editOpen = false;
      this.resetForm("editForm");
    },
    // 搜索按钮操作
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    //提交按钮
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          const params = {
            ...this.form,
            groupId: this.groupId,
          };
          debugger;
          addProductInGroup(params).then((res) => {
            if (res.code == 200) {
              this.msgSuccess(res.msg);
              this.open = false;
              this.getList();
            } else {
              this.$message.error(res.msg);
            }
          });
        }
      });
    },
    // 编辑
    handleUpdate(row) {
      this.editOpen = true;
      this.editForm = {
        groupId: row.id,
        groupName: row.groupName,
      };
    },
    // 重命名提交
    submitEdit() {
      this.$refs["editForm"].validate((valid) => {
        if (valid) {
          const params = {
            ...this.editForm,
            // groupId: this.groupId
          };
          debugger;
          modifyGroupName(params).then((res) => {
            if (res.code == 200) {
              this.msgSuccess(res.msg);
              this.editOpen = false;
              this.getList();
            } else {
              this.$message.error(res.msg);
            }
          });
        }
      });
    },
    // 删除组合
    handleDelete(row) {
      this.$confirm("是否确认删除选中的数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return deleteGroup({ groupId: row.id });
        })
        .then((res) => {
          if (res.code == 200) {
            this.queryParams.pageNum = 1;
            this.getList();
            this.msgSuccess("删除成功");
          } else {
            this.$message.error(res.msg);
          }
        })
        .catch(() => {});
    },
    // 清空
    clear() {
      (this.queryParams.findString = ""), this.handleQuery();
    },
  },
};
</script>

<style lang="scss" rel="stylesheet/scss"  scoped>
.search-content {
  display: flex;
  // background-image: url("../../../assets/images/title.png") no-repeat;
  height: 50px;
  width: 100%;
  background: #fff;
  font-size: 18px;
  font-family: Source Han Sans CN, Source Han Sans CN-Medium;
  font-weight: 600;
  text-align: left;
  color: #333333;
  border-bottom: 2px solid #f5f7fa;
  justify-content: space-between;
  .search-input {
    width: 180px;
    height: 30px;
    // margin: auto 20px;
    margin-left: 10px;
  }
}
::v-deep .search-input .el-input__inner {
  // border-radius: 16px;
  border: 0px;
}
.tag-item {
  width: 33%;
  margin-bottom: 10px;
  display: flex;
  justify-content: center;
}
.button-new-tag {
  margin-bottom: 10px;
  margin: auto;
  width: 100px;
}
.tag-content {
  // width: 80%;
  margin: auto 120px;
  display: flex;
  flex-wrap: wrap;
}

// 下拉框
.select-header-popper {
  max-width: 300px !important;
  min-width: 100px !important;
  .el-select-dropdown__wrap {
    overflow-y: auto !important;
  }
}
</style>